#!/usr/bin/env python
import os
import sys
import subprocess
try:
    from nose import run
except ImportError:
    print 'You need to have `nose` installed to run Rammbock-ASN.1 unit tests.'
    print 'Download nose from http://somethingaboutorange.com/mrl/projects/nose'
    run = None

BASEDIR = os.path.dirname(__file__)

def _set_resources_to_erlang_path():
    erl_path = os.environ.get('ERL_LIBS','')
    erl_path = os.path.join(BASEDIR,'..')+os.pathsep+erl_path
    os.environ['ERL_LIBS'] = erl_path
    os.putenv('ERL_LIBS', erl_path)

if __name__ == '__main__':
    if run:
        _set_resources_to_erlang_path()
        sys.path.insert(0, os.path.join(BASEDIR, '..', 'src'))
        tests_passed = run(defaultTest=BASEDIR)
        retcode = 0 if tests_passed else 1
        subprocess.call(['erl_call', '-sname', 'rammbock', '-q'])
        subprocess.call(['epmd', '-kill'])
        sys.exit(retcode)
